package sz.szsmk.citizencard.ui.adapter;

import android.util.Log;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import sz.szsmk.citizencard.R;
import sz.szsmk.citizencard.log.L;
import sz.szsmk.citizencard.model.network.bean.response.QueryBusRouteRes;
import sz.szsmk.citizencard.model.network.bean.response.TestBean;
import sz.szsmk.citizencard.util.Constant;

/**
 * Copyright (c)2018
 * author: zdh
 * created on: 2018/11/21 17:41
 * description:
 */
public class IndicatorExpandableListAdapter extends BaseExpandableListAdapter {
    private static final String TAG = "IndicatorExpandableList";
    private List<TestBean.LineListBean> groupData;
    private List<TestBean.LineListBean> childData;
    //                用于存放Indicator的集合
    private SparseArray<ImageView> mIndicators;
    private OnGroupExpandedListener mOnGroupExpandedListener;
    private List<QueryBusRouteRes.BusRouteListBean> viewData = new ArrayList<>();
    private OnZdClickListener onZdClickListener;

    public interface OnZdClickListener {

        void onZdClick(int poisiton);
    }

    public IndicatorExpandableListAdapter(List<TestBean.LineListBean> groupData, List<TestBean.LineListBean> childData) {
        this.groupData = groupData;
        this.childData = childData;
        mIndicators = new SparseArray<>();
    }

    public void setOnGroupExpandedListener(OnGroupExpandedListener onGroupExpandedListener) {
        mOnGroupExpandedListener = onGroupExpandedListener;
    }

    public void setOnZdClickListener(OnZdClickListener onZdClickListener) {
        this.onZdClickListener = onZdClickListener;
    }

    //            根据分组的展开闭合状态设置指示器
    public void setIndicatorState(int groupPosition, boolean isExpanded) {
        if (isExpanded) {
            mIndicators.get(groupPosition).setImageResource(R.drawable.arrow_up);
        } else {
            mIndicators.get(groupPosition).setImageResource(R.drawable.arrow_below);
        }
    }

    @Override
    public int getGroupCount() {
        return viewData.size();
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return 1;
    }

    @Override
    public Object getGroup(int groupPosition) {
        return viewData.get(groupPosition);
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return viewData.get(groupPosition);
//        return childData[groupPosition][childPosition];
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public boolean hasStableIds() {
        return true;
    }

    @Override
    public View getGroupView(final int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        GroupViewHolder groupViewHolder;


        if (convertView == null) {
            convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_expand_group_indicator, parent, false);
            groupViewHolder = new GroupViewHolder();
            groupViewHolder.tvTitle = (TextView) convertView.findViewById(R.id.label_group_indicator);
            groupViewHolder.line_desbus = (TextView) convertView.findViewById(R.id.line_desbus);
            groupViewHolder.line_destime = (TextView) convertView.findViewById(R.id.line_destime);
            groupViewHolder.round = (TextView) convertView.findViewById(R.id.round);

            groupViewHolder.ivIndicator = (ImageView) convertView.findViewById(R.id.iv_indicator);
            convertView.setTag(groupViewHolder);
        } else {
            groupViewHolder = (GroupViewHolder) convertView.getTag();
        }

        if (viewData != null) {
            List<QueryBusRouteRes.BusRouteListBean.BusRouteCountListBean> busRouteCountList = viewData.get(groupPosition).getBusRouteCountList();


            if (viewData.get(groupPosition).getBus_route_intime() != null) {
                groupViewHolder.round.setBackgroundResource(R.drawable.bus2);

                L.e(Constant.TAG, "进站时间--------------------------" + viewData.get(groupPosition).getBus_route_intime());
                groupViewHolder.line_desbus.setVisibility(View.VISIBLE);
                groupViewHolder.line_desbus.setText(viewData.get(groupPosition).getBus_route_number() +" 进站时间 "+viewData.get(groupPosition).getBus_route_intime());
            } else {
                groupViewHolder.round.setBackgroundResource(R.drawable.kongxinyuan);

                groupViewHolder.line_desbus.setVisibility(View.GONE);
            }

        }

        groupViewHolder.tvTitle.setText(viewData.get(groupPosition).getBus_route_stand());
        groupViewHolder.tvTitle.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onZdClickListener.onZdClick(groupPosition);
            }
        });
        //      把位置和图标添加到Map
        mIndicators.put(groupPosition, groupViewHolder.ivIndicator);
        //      根据分组状态设置Indicator
        setIndicatorState(groupPosition, isExpanded);
        return convertView;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View
            convertView, ViewGroup parent) {
        ChildViewHolder childViewHolder;

        if (convertView == null) {
            convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_expand_child, parent, false);
            childViewHolder = new ChildViewHolder();
            childViewHolder.tvTitle = (TextView) convertView.findViewById(R.id.label_expand_child);
            childViewHolder.intime = (TextView) convertView.findViewById(R.id.real_time_item_time);
            childViewHolder.num1 = ((TextView) convertView.findViewById(R.id.real_time_item_num1));
            childViewHolder.num2 = ((TextView) convertView.findViewById(R.id.real_time_item_num2));
            childViewHolder.num3 = ((TextView) convertView.findViewById(R.id.real_time_item_num3));
            childViewHolder.line = ((TextView) convertView.findViewById(R.id.line_hor));
            childViewHolder.childLl = (LinearLayout) convertView.findViewById(R.id.child_ll);
            childViewHolder.itemView = (LinearLayout) convertView.findViewById(R.id.bus_ll);
            convertView.setTag(childViewHolder);
        } else {
            childViewHolder = (ChildViewHolder) convertView.getTag();
        }
        List<QueryBusRouteRes.BusRouteListBean.BusRouteCountListBean> busRouteCountList = viewData.get(groupPosition).getBusRouteCountList();
        if (busRouteCountList == null || busRouteCountList.size() == 0) {
            childViewHolder.num1.setText("--");
            childViewHolder.num2.setText("--");
            childViewHolder.num3.setText("--");
            childViewHolder.line.setVisibility(View.GONE);
            childViewHolder.itemView.setVisibility(View.GONE);


        } else if (busRouteCountList != null) {
//            String bus_route_number = viewData.get(groupPosition).getBus_route_number();
//            String bus_route_intime = viewData.get(childPosition).getBus_route_intime();
            childViewHolder.tvTitle.setText(viewData.get(groupPosition).getBus_route_number());
            if (viewData.get(groupPosition).getBus_route_intime() != null) {
                childViewHolder.intime.setText("进站时间：" + viewData.get(groupPosition).getBus_route_intime());
            }
            childViewHolder.line.setVisibility(View.VISIBLE);
            childViewHolder.itemView.setVisibility(View.VISIBLE);
            childViewHolder.childLl.setVisibility(View.VISIBLE);
            if (busRouteCountList.size() == 1) {
                childViewHolder.num1.setText(busRouteCountList.get(0).getBus_route_count());
                childViewHolder.num2.setText("--");
                childViewHolder.num3.setText("--");
            }
            if (busRouteCountList.size() == 2) {
                childViewHolder.num1.setText(busRouteCountList.get(0).getBus_route_count());
                childViewHolder.num2.setText(busRouteCountList.get(1).getBus_route_count());
                childViewHolder.num3.setText("--");

            }
            if (busRouteCountList.size() == 3) {
                childViewHolder.num1.setText(busRouteCountList.get(0).getBus_route_count());
                childViewHolder.num2.setText(busRouteCountList.get(1).getBus_route_count());
                childViewHolder.num3.setText(busRouteCountList.get(1).getBus_route_count());
            }

        }

        return convertView;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }

    @Override
    public void onGroupExpanded(int groupPosition) {
        Log.d(TAG, "onGroupExpanded() called with: groupPosition = [" + groupPosition + "]");
        if (mOnGroupExpandedListener != null) {
            mOnGroupExpandedListener.onGroupExpanded(groupPosition);
        }
    }

    @Override
    public void onGroupCollapsed(int groupPosition) {
        Log.d(TAG, "onGroupCollapsed() called with: groupPosition = [" + groupPosition + "]");
    }

    public void setViewData(List<QueryBusRouteRes.BusRouteListBean> viewData) {
        this.viewData = viewData;
    }

    private static class GroupViewHolder {
        TextView tvTitle;
        TextView line_desbus;
        TextView line_destime;
        TextView round;
        ImageView ivIndicator;
    }

    private static class ChildViewHolder {
        TextView tvTitle;
        TextView intime;
        TextView num1;
        TextView num2;
        TextView num3;
        TextView line;
        LinearLayout childLl;
        LinearLayout itemView;
    }
}
